var DBGridFuncs = new Object();
opal.widget.DBGrid = function (options){
	options.id = $pick(options.id, opal.generateId());

	var grid = Element('table', {
		id: options.id,
		'width': '100%',
		'border': 1,
		'cellpadding': 0,
		'cellspacing': 0,
		'align': 'center',
		'bordercolorlight': '#999999',
		'bordercolordark': '#FFFFFF'
	});
	opal.widget.Initialize.call(grid, options);
	
	grid.setOptions({
		id: $pick(options.id,opal.generateId()),
		data: $pick(options.data,null),
		summaryDataName: $pick(options.summaryDataName),
		descColumnsName: $pick(options.descColumnsName),
		columns: new Array(),
		allcolumns: new Array(),
		header: null,
		footer: null,
		rows: new Array(),
		hiddenrow: null,
		indicator: $pick(options.indicator, null),
		isDargable: $pick(options.dragable, false),
		headerItemClone: null,
		sourHeaderItem: null,
		targetHeaderItem: null
		
	});
	
	
	$extend(grid, DBGridFuncs);
	//grid.setColumns(eval("options.data." + options.descColumnsName));
	var hiddenrow = new Element('tr',{
		'style': 'display:none;'
	});
	
	grid.options.hiddenrow = hiddenrow;
	opal.widget.Render.call(grid, grid);
	return grid;
};

DBGridFuncs.addColumn = function (columnDes){
	var column = new opal.widget.ColumnDesc(columnDes);
	this.options.columns.include(column);
	
	return column;
};

DBGridFuncs.setColumns = function (descColumns){
	if(!descColumns){
		return;
	}
	this.options.columns = new Array();
	this.options.allcolumns = new Array();
	this.options.rows = new Array();
	var columns = this.options.columns;
	descColumns.each(function (item){
		var column = new opal.widget.ColumnDesc(item);
		columns.include(column);
	});
};

DBGridFuncs.addColumnBefore = function (column){
	
};

DBGridFuncs.deleteColumn = function (column){
	
};

DBGridFuncs.getColumnsField = function (){
	var fields = new Array();
	var columns = this.options.columns;
	
	columns.each(function (column, index){
		
	});
};

DBGridFuncs.getColumns = function (){
	return this.options.columns;
};

DBGridFuncs.getSummaryData = function (){
	var data = this.options.data;
	if(!data) return null;
	var summaryDataName = this.options.summaryDataName;
	return eval("data." + summaryDataName);
};


DBGridFuncs.setData = function (data){
	this.options.data = data;
	this.setColumns(eval("data." + this.options.descColumnsName));
};

DBGridFuncs.clear = function (){
	this.empty();
};

DBGridFuncs.reload = function (data){
	if(data){
		this.setData(data);
	}
	this.clear();
	this.display();
};

DBGridFuncs.setDefaultSelected = function (num){
	if(num != 0){
		if(!num || num < 0 || num > this.options.rows.length-1){
			return;
		}
	}
	var rows = this.options.rows;
	var row = rows[num];
	if(row){
		
		row.fireEvent('click');
		//this.indicatorClick(this);
	}
};

DBGridFuncs.display = function (){
	//this.setColumns(eval("this.options.data." + this.options.descColumnsName));
	var data = this.getSummaryData();
	var alldata = this.options.data;
	var columns = this.getColumns();
	var allcolumns = this.options.allcolumns;
	var rows = this.options.rows;
	if(data){
		
		var datahash = new Hash(data.getLast());
		datahash.each(function(value, key){
			allcolumns.include(new opal.widget.ColumnDesc({'display': key, 'field': key}));
		});
	}
	if(!columns.getLast()){
		if(data){
			var datahash = new Hash(data.getLast());
			datahash.each(function(value, key){
				columns.include(new opal.widget.ColumnDesc({'display': key, 'field': key}));
			});
		}
	}
	var header = new opal.widget.Header(this.options.indicator);
	header.setDBGrid(this);
	var theader = header.display(this.options.columns);
	
	theader.inject(this);
	this.options.header = theader;
	
	var tbody = new Element('tbody');
	var indicator = this.options.indicator;
	var dbgrid = this;
	if(data){
		data.each(function(item,index){
			var row = new opal.widget.Row({'rownum': index, 'data': item, 'indicator': indicator, 'dbgrid': dbgrid});
			row.display(columns).inject(tbody);	
			rows.include(row);
			
		});
	}
	
	
	var hasPre = false;
	var hasNext = false;
	var nCurRowCount = 0;
	
	
	var strCurOrderByCol = null;
	var strCurOrderByType = 'DESC';
	var nCurMaxItem = null;
	var nCurPageNo = 0;
	var nCurCount = 0;
	var nCurTotalPage = 0;
	var nCurEndIndex = 0;
	
	if(alldata){
		strCurOrderByCol = $pick(eval("alldata.orderbycolumn"), null);
		strCurOrderByType = $pick(eval("alldata.orderbytype"), 'DESC');
		nCurMaxItem = $pick(eval("alldata.maxitem"), null);
		nCurPageNo = $pick(eval("alldata.currentpage"), 0);
		nCurCount = $pick(eval("alldata.count"), 0);
		nCurTotalPage = $pick(eval("alldata.totalpage"), 0);
		nCurEndIndex = $pick(eval("alldata.theendindex"), 0);
		
		if(parseInt(nCurCount) != 0 && eval("alldata.haspre")) hasPre = eval("alldata.haspre");
		if(eval("alldata.hasnext")) hasNext = eval("alldata.hasnext");
	}
	
	
	
	if(parseInt(nCurPageNo) < parseInt(nCurTotalPage) && parseInt(nCurPageNo) > 0){
		nCurRowCount = nCurMaxItem;
	}else if(parseInt(nCurPageNo) == parseInt(nCurTotalPage) && parseInt(nCurTotalPage) != 0 ){
		nCurRowCount = parseInt(nCurCount) % parseInt(nCurMaxItem);
		
		if(nCurRowCount == 0){
			nCurRowCount = parseInt(nCurMaxItem);
		}
	}
	
	
	//var hiddentd = new Element('td',{
	//	'colspan': 30
	//});
	
	
	
	//hiddentd.inject(this.options.hiddenrow);
	//this.options.hiddenrow.inject(tbody);
	
	var footer = new opal.widget.Footer({
		'strCurOrderByCol': strCurOrderByCol, 
		'strCurOrderByType': strCurOrderByType,
		'nCurMaxItem': nCurMaxItem,
		'nCurPageNo': nCurPageNo,
		'nCurCount': nCurCount,
		'nCurTotalPage': nCurTotalPage,
		'nCurEndIndex': nCurEndIndex,
		'bCurHasPre': hasPre,
		'bCurHasNext': hasNext,
		'nCurRowCount': nCurRowCount
	});
	footer.inject(tbody);
	tbody.inject(this);
	
	if(this.options.isDargable){
		this.makeHeaderDargable();
	}
	
	
};

DBGridFuncs.makeHeaderDargable = function(){
	var header = this.options.header;
	var drop = header.getElements('td');
 	var dbgrid = this;
	header.getElements('td').each(function(item, index){
 		if(dbgrid.options.indicator){
			if(index != 0){
				item.addEvent('mousedown', function(e) {
					e = new Event(e).stop();
					var drag = null;
			 		dbgrid.options.sourHeaderItem = this;
					dbgrid.options.headerItemClone = new Element('div')	
						.set('html', this.get('html'))
						.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
						.setStyles({'opacity': 0.7, 'position': 'absolute'})
						.addEvent('emptydrop', function() {this.remove();})
						.addEvent('blur', function() {this.remove();})
						.addEvent('mouseout', function() {this.remove();})
						.inject(document.body);
			 		
					drag = dbgrid.options.headerItemClone.makeDraggable({
						droppables: [drop]
					}); // this returns the dragged element
			 
					drag.start(e); // start the event manual
				});
			}		
 		}
		
	 
	});
};

DBGridFuncs.setSourHeaderItem = function(sourHeaderItem){
	this.options.sourHeaderItem = sourHeaderItem;
};

DBGridFuncs.setTargetHeaderItem = function(targetHeaderItem){
	this.options.targetHeaderItem = targetHeaderItem;
};

DBGridFuncs.addHiddenItem = function(options){
	var hidden = new opal.widget.Hidden(options);
	hidden.inject($('hiddenTd'));
	return hidden; 
};

DBGridFuncs.setHiddenItemValue = function(name, value){
	var hidden = $(name);
	if(!hidden){
		alert('no element found that id is :' + name);
		return;
	}
	hidden.set('value', value);
	return hidden; 
};

DBGridFuncs.showAllColumnsDiv = function(){
	var hidden = new opal.widget.AllColumnDiv({'allcolumns': this.options.allcolumns, 'columns': this.options.columns, 'DBGrid': this});
	var footer = hidden.display();
	footer.inject(document.body);
	return footer; 
};


DBGridFuncs.changeCloumn = function(){
	var temp = null;
	var sour = this.options.sourHeaderItem;
	var target = this.options.targetHeaderItem;
	
	if(sour == target){
		return;
	}
	
	temp = sour.get('html');
	sour.set('html', target.get('html'));
	target.set('html', temp);
	
	var sourcolumnnum = sour.options.columnnum;
	var targetcolumnnum = target.options.columnnum;
	
	var sourItems = this.getItemsByColumnnum(sourcolumnnum);
	var targetItems = this.getItemsByColumnnum(targetcolumnnum);
	
	sourItems.each(function(sourItem, index){
		var targetItem = targetItems[index];
		temp = sourItem.get('html');
		sourItem.set('html', targetItem.get('html'));
		targetItem.set('html', temp);
	});
};

DBGridFuncs.getItemsByColumnnum = function(columnnum){
	var items = new Array();
	
	this.options.rows.each(function(row, index){
		row.options.items.each(function(item, index){
			if(item.options.columnnum == columnnum){
				items.include(item);
			}
		});
	});
	
	return items;
};

DBGridFuncs.rowClick = function (row){
	
}

DBGridFuncs.indicatorClick = function (indicator){
	
}


var ColumnDescFuns = new Object();

opal.widget.ColumnDesc = function (options){
	
	var columnDesc = new Element('div');
	
	opal.widget.Initialize.call(columnDesc, options);
	
	columnDesc.setOptions({
		field: options.field,
		display: options.display,
		type : $pick(options.displayType, 'text'),
		href :  $pick(options.href, '#'),
		isVisible: $pick(options.isVisible, 'true')
	});
	
	$extend(columnDesc, ColumnDescFuns);

	return columnDesc;
};

ColumnDescFuns.getDisplay = function (){
	return this.options.display;
};

ColumnDescFuns.setDisplay = function (display){
	this.options.display = display;
};

ColumnDescFuns.getField = function (){
	return this.options.field;
};

ColumnDescFuns.setField = function (field){
	this.options.field = field;
};

ColumnDescFuns.getType = function (){
	return this.options.type;
};

ColumnDescFuns.setType = function (type){
	this.options.type = type;
};

ColumnDescFuns.isVisible = function (){
	return this.options.isVisible;
};

ColumnDescFuns.setVisible = function (visible){
	this.options.isVisible == visible;
};

ColumnDescFuns.getHref = function (){
	return this.options.href;
};

ColumnDescFuns.setHref = function (href){
	this.options.href = href;
};

var HeaderFuns = new Object();

opal.widget.Header = function (indicator){
	var header = new Element('thead',{
		
	});
	
	opal.widget.Initialize.call(header, {});
	header.setOptions({
		'indicator': indicator,
		'DBGrid': null
	});
	
	$extend(header,HeaderFuns);
	
	header.addEvent('mousedown', header.mousedownFunc.bind(header));
	header.addEvent('mouseover', header.mouseoverFunc.bind(header));
	header.addEvent('mouseout', header.mouseoutFunc.bind(header));
	header.addEvent('mouseup', header.mouseupFunc.bind(header));

	return header;
};

HeaderFuns.setDBGrid = function (dbgrid){
	this.options.DBGrid = dbgrid;
};

HeaderFuns.getDBGrid = function (dbgrid){
	return this.options.DBGrid;
};

HeaderFuns.display = function (columns){
	var header = this;
	var tr = new Element('tr',{
		id: opal.generateId(),
		'class': 'deepblue-left'
	});
	
	
	
	if(this.options.indicator){
		var headIndicator = new Element('td',{
			id: opal.generateId(),
			'width': '5%',
			'text': ' '
		});
		headIndicator.inject(tr);
	}
	columns.each(function(item,index){
		var headItem = new opal.widget.HeadItem({'display': item.getDisplay(), 'field': item.getField(), 'isVisible': item.isVisible(), 'columnnum': index, 'DBGrid': header.getDBGrid()});
		headItem.inject(tr);
	});

	tr.inject(this);	
	return this;
}

HeaderFuns.mousedownFunc = function () {

}

HeaderFuns.mouseoverFunc = function () {

}

HeaderFuns.mouseoutFunc = function () {

}

HeaderFuns.mouseupFunc = function () {

}

var HeadItemFuncs = new Object();

opal.widget.HeadItem = function (options){
	var headItem = new Element('td',{
		id: opal.generateId(),
		'class': 'title_center',
		'width': '200',
		'text' : options.display
	});
	headItem.cloneItem = null;
	headItem.field = options.field;
	
	opal.widget.Initialize.call(headItem, options);
	headItem.setOptions({
		'columnnum': options.columnnum,
		'DBGrid': options.DBGrid
	});
	
	
	$extend(headItem,HeadItemFuncs);
	
	headItem.addEvent('click', headItem.clickFunc.bind(headItem));
	headItem.addEvent('mousedown', headItem.mousedownFunc.bind(headItem));
	headItem.addEvent('mouseup', headItem.mouseupFunc.bind(headItem));
	headItem.addEvent('mouseover', headItem.mouseoverFunc.bind(headItem));
	headItem.addEvent('mouseout', headItem.mouseoutFunc.bind(headItem));
	headItem.setStyle('cursor', 'pointer');

	if(options.isVisible == 'false'){
		headItem.setStyle('display','none');
	}
	
	headItem.addEvents({
			'drop': function() {	
				options.DBGrid.setTargetHeaderItem(headItem);
				options.DBGrid.changeCloumn();
				options.DBGrid.options.headerItemClone.remove();
				headItem.setStyle('background', '');
				headItem.setStyle('background-color', '#90AAC5');
			},
			'over': function() {
				headItem.setStyle('background', 'url(img/gif-0068.gif) 0px 4px no-repeat');
			},
			'leave': function() {
				headItem.setStyle('background', '');
				headItem.setStyle('background-color', '#90AAC5');
			}
		});
	
	return headItem;
	
}

HeadItemFuncs.clickFunc = function () {
	if(this.options.DBGrid.options.headerItemClone){
		this.options.DBGrid.options.headerItemClone.remove();
	}
	page_summary('orderby',this.field);
}

HeadItemFuncs.mousedownFunc = function () {
	
	
}

HeadItemFuncs.mouseupFunc = function () {
}

HeadItemFuncs.mouseoverFunc = function () {
	
}

HeadItemFuncs.mouseoutFunc = function () {
	
}

var RowFuns = new Object();

opal.widget.Row = function (options){
	var row = new Element('tr',{
		id: opal.generateId()
	});
	
	opal.widget.Initialize.call(row, {});
	
	row.setOptions({
		rownum: $pick(options.rownum),
		items : new Array(),
		data: $pick(options.data),
		indicator: $pick(options.indicator),
		indicatorItem: null,
		dbgrid: options.dbgrid
	});
	
	$extend(row,RowFuns);
	
	if(row.options.rownum % 2 == 1){
		row.addClass('td-listrom');
	}
	
	row.addEvent('mouseover', row.mouseoverFunc.bind(row));
	row.addEvent('mouseout', row.mouseoutFunc.bind(row));
	row.addEvent('click', row.clickFunc.bind(row));

	return row;
};

RowFuns.display = function (columns){
	
	var data = this.options.data;
	var dataHash = new Hash(data);
	var tr = this;
	var indicator = this.options.indicator;
	if(indicator){
		var value = dataHash.get(indicator.dataIdColumn);
		var checked = '';
		if(this.getRowNum() == 0){
			checked = 'checked';
		}
		var indicator = new opal.widget.Indicator(indicator, value, this.options.dbgrid, checked, this.options.rownum);
		this.options.indicatorItem = indicator;
		indicator.inject(this);
	}

	columns.each(function(item,index){
		var rowItem = new opal.widget.Cell({'columnnum': index, 'rownum': tr.options.rownum, 'content':  dataHash.get(item.options.field), 'column': item, 'rowValue': dataHash});
		rowItem.inject(tr);
		tr.options.items.include(rowItem);
	});
	
	
	return this;
}

RowFuns.mouseoverFunc = function (){
	this.addClass('tr-over');
}

RowFuns.mouseoutFunc = function (){
	this.removeClass('tr-over');
}

RowFuns.getIndicator = function (){
	return this.options.indicatorItem;
}

RowFuns.getDBGrid = function (){
	return this.options.dbgrid;
}

RowFuns.getRowNum = function (){
	return this.options.rownum;
}

RowFuns.getData = function (){
	return new Hash(this.options.data);
}

RowFuns.clickFunc = function (){
	this.getIndicator().fireEvent('click');
	this.getDBGrid().rowClick(this);
}

var CellFuncs = new Object();

opal.widget.Cell = function (options){
	var cell = new Element('td', {
		id: opal.generateId(),
		'width': $pick(options.width),
		'text':  ' ' + options.content
	});
	
	opal.widget.Initialize.call(cell, options);
	
	cell.setOptions({
		rownum: options.rownum,
		columnnum: options.columnnum,
		content: options.content,
		'columnDes': options.column,
		'rowValue': options.rowValue
	});
	
	var desc = options.column;
	
	
	if(desc.isVisible() == 'false'){
		cell.setStyle('display', 'none');
		desc.setOptions({'value': options.content, 'rownum': options.rownum});
		cell.set('text', '');
		var hidden = new opal.widget.HiddenItem(desc);
		hidden.inject(cell);
	}
	
	var type = desc.getType();
	
	if(type == 'a'){
		var href = desc.getHref();
		var hash = cell.options.rowValue;
		var frist = href.split('[');
		var newString = "";
		frist.each(function (item1){
			var sec = item1.split(']');
			sec.each(function (item2, index){
				if(item2){
					if(hash.has(item2)){
						var value = hash.get(item2);
						newString = newString + value;
					}else {
						newString = newString + item2;
					}
				}
			});
		});
		var innerhtml = '<a href="'+ newString + '">' + ' ' + options.content + '</a>';
		
		cell.set('html',innerhtml);
	}
	
	return cell;
};

CellFuncs.onclick = function (){
	
};

CellFuncs.setWidth = function (){
	
};

CellFuncs.display = function (){
	this.setProperties({
		text : this.options.content
	});
	
	return this;
};

var IndicatorFuncs = new Object();


opal.widget.Indicator = function (options, value, dbgrid,checked, rownum){
	var indicator = new Element('td',{
		id: $pick(options.id, opal.generateId())
	});
	
	indicator.value = value;
	
	var indicatorItem = new Element('input',{
		'type': $pick(options.type,'radio'),
		id: $pick(options.id, opal.generateId()),
		'class': $pick(options.classname, 'normalradio'),
		'name': $pick(options.name, 'indicator'),	
		'value': $pick(value, -1),
		'checked': checked
	});
	indicatorItem.inject(indicator);
	$extend(indicator, IndicatorFuncs);
	indicatorItem.dbgrid = dbgrid;
	indicatorItem.rownum = rownum;
	indicator.addEvent('click', indicator.clickFunc.bind(indicatorItem));
	indicator.indicatorItem = indicatorItem;
	
	return indicator;
}

IndicatorFuncs.getValue = function (){
	return this.value;
}

IndicatorFuncs.clickFunc = function (){
	this.checked = 'checked';
	this.dbgrid.indicatorClick(this);
}


var HiddenItemFuncs = new Object();

opal.widget.HiddenItem = function (options) {
	var hiddenItem = new Element('input',{
		id: $pick(options.id, opal.generateId()),
		'type': 'hidden',
		'value': $pick(options.options.value, ''),
		'name' : $pick(options.name, options.getField()) + options.options.rownum
	});
	
	$extend(hiddenItem, HiddenItemFuncs);
	
	return hiddenItem;
}

HiddenItemFuncs.setName = function (name){
	this.options.name = name;
};

var FooterFuncs = new Object();

opal.widget.Footer = function (options){
	var footer = new Element('tr');
	
	opal.widget.Initialize.call(footer, options);
	
	footer.setOptions({
		nCurPageNo: $pick(options.nCurPageNo, 0),
		bCurHasPre: $pick(options.bCurHasPre, false),
		bCurHasNext: $pick(options.bCurHasNext, false),
		nCurTotalPage: $pick(options.nCurTotalPage, 0),
		nCurEndIndex: $pick(options.nCurEndIndex, 0),
		nCurMaxItem: $pick(options.nCurMaxItem, null),
		strCurOrderByCol: $pick(options.orderbycolumn, null),
		strCurOrderByType: $pick(options.orderbytype, 'DESC'),
		nCurRowCount: $pick(options.rowCount, 0),
		nCurCount: $pick(options.nCurCount, 0),
		rootPath: rootPath,
		languageCode: languageCode,
		lnumber: lnumber,
		lpage: lpage,
		ltotal: ltotal
		
		
	});
	
	$extend(footer, FooterFuncs);
	
	var td = new Element('td', {
		'colspan': 30,
		'id': 'hiddenTd'
	});
	
	var htheendindex = new opal.widget.Hidden({'name': 'theendindex', 'value': options.nCurEndIndex});
	var hmaxitem = new opal.widget.Hidden({'name': 'maxitem', 'value': options.nCurMaxItem});
	var hpageaction = new opal.widget.Hidden({'name': 'pageaction'});
	var horderbycolumn = new opal.widget.Hidden({'name': 'orderbycolumn', 'value': options.strCurOrderByCol});
	var horderbytype = new opal.widget.Hidden({'name': 'orderbytype', 'value': options.strCurOrderByType});
	var htotalpage = new opal.widget.Hidden({'name': 'totalpage', 'value': options.nCurTotalPage});
	var hrowcount = new opal.widget.Hidden({'name': 'rowcount', 'value': options.nCurRowCount});
	var hispasscheck = new opal.widget.Hidden({'name': 'ispasscheck'});
	
	htheendindex.inject(td);
	hmaxitem.inject(td);
	hpageaction.inject(td);
	horderbycolumn.inject(td);
	horderbytype.inject(td);
	htotalpage.inject(td);
	hrowcount.inject(td);
	hispasscheck.inject(td);
	
	var table = new Element('table', {
		'width': '100%',
		'cellspacing': 0,
		'border': 0
	});
	
	var tbody = new Element('tbody');
	
	var tr =  new Element('tr');
	
	var td1 = new Element('td',{
		'height': '19'
	});
	
	td1.set('html', '<div align="right"><span class="td_right"> </span> </div>');
	td1.inject(tr);
	var td2 = new Element('td',{
		'height': '19',
		'width': '53'
	});
	
	if(parseInt(footer.options.nCurPageNo) > 1){
		td2.set('html', '<a href="javascript:page_summary(' + "'first'" + ')"><span class="td_right"><img src="'+ rootPath + '/images/' + languageCode + '/first.gif" style="border: 0 solid #000000" border="0"></span></a>');
	}

	if(footer.options.bCurHasPre == 'true'){
		td2.set('html', td2.get('html') + '<span class="td_right"><a href="javascript:page_summary(' + "'pre'" + ')"><img src="'+ rootPath + '/images/' + languageCode + '/Pre.gif" border="0" style="border: 0 solid #000000"></a></span>');
	}
	td2.inject(tr);
	var td3 = new Element('td',{
		'height': '19',
		'width': '40',
		'class': 'td_right',
		'text': footer.options.lnumber
	});
	td3.inject(tr);
	var td4 = new Element('td',{
		'height': '19',
		'width': '32',
		'class': 'td_center'
	});
	
	td4.set('html', '<input type="text" name="jumptemp" size="3" value="' + footer.options.nCurPageNo + '" onblur="javascript:jumppage.value = jumptemp.value;"><input type="hidden" name="jumppage" size="3" value="' + footer.options.nCurPageNo + '">');
	td4.inject(tr);
	
	var td5 = new Element('td',{
		'height': '19',
		'width': '17',
		'class': 'td_right',
		'text': footer.options.lpage
	});
	td5.inject(tr);
	var td6 = new Element('td',{
		'height': '19',
		'width': '43'
	});
	
	td6.set('html', '<a href="javascript:page_summary(' + "'jumppage'" + ')"><img src="'+ rootPath + '/images/' + languageCode + '/go_fy.gif" width="25" height="12" border="0"></a>');
	td6.inject(tr);
	var td7 = new Element('td',{
		'height': '19',
		'width': '85',
		'class': 'td_center',
		text: footer.options.ltotal+ ' ' + footer.options.nCurTotalPage + ' ' + footer.options.lpage
	});
	
	td7.inject(tr);
	var td8 = new Element('td',{
		'height': '19',
		'width': '81',
		'class': 'td_right'
	});

	if(footer.options.bCurHasNext == 'true'){
		td8.set('html', '<a href="javascript:page_summary(' + "'next'" + ')"><img src="'+ rootPath + '/images/' + languageCode + '/next.gif" style="border: 0 solid #000000" border="0"></a>');
	}
	if(parseInt(footer.options.nCurPageNo) < parseInt(footer.options.nCurTotalPage)){
		td8.set('html', td8.get('html') + '<a href="javascript:page_summary(' + "'last'" + ')"><img src="'+ rootPath + '/images/' + languageCode + '/last.gif" style="border: 0 solid #000000" border="0"></a>');

	}
	td8.inject(tr);
	tr.inject(tbody);
	tbody.inject(table);
	table.inject(td);
	td.inject(footer);
	return footer;
}

opal.widget.Hidden = function (options){
	var hidden = new Element('input',{
		name: $pick(options.name),
		id: $pick(options.name),
		type: 'hidden',
		value: $pick(options.value, '')
	});
	
	return hidden;
}
var AllColumnDivFuncs = new Object();
opal.widget.AllColumnDiv = function (options){
	var allColumnDiv = new Element('div',{});
	opal.widget.Initialize.call(allColumnDiv, options);
	allColumnDiv.setOptions({
		columns: $pick(options.columns,new Array()),
		allcolumns: $pick(options.allcolumns,new Array()),
		DBGrid: $pick(options.DBGrid)
	});
	allColumnDiv.drag = null;
	allColumnDiv.setStyles({
		'width': '300px',
		'height': '150px',
		'border-style': 'solid',
		'border-width': '1',
		'vertical-align': 'middle',
		'position': 'absolute'
	});
	
	$extend(allColumnDiv, AllColumnDivFuncs);
	
	return allColumnDiv;
};

AllColumnDivFuncs.makeDrag = function (e){
	var drag = this.makeDraggable({
						onComplete: function(){
							
							
					    }			
					}); // this returns the dragged element
			 
	drag.start(e); // start the event manual
};

AllColumnDivFuncs.unDrag = function (){
	this.removeEvents();
};

AllColumnDivFuncs.display = function (){
	this.displayAllColumnDivHeader();
	this.displayAllColumnDivBody();
	this.displayAllColumnDivFooter();
	
	return this;
};

AllColumnDivFuncs.displayAllColumnDivHeader = function (){
	var headerDiv = new Element('div',{
		id: opal.generateId(),
		'class': 'headerDiv'
	});
	
	headerDiv.addEvent('mouseover', function (){
		this.setStyle('cursor', 'move');
	});
	
	headerDiv.addEvent('mousedown', this.makeDrag.bind(this));
	headerDiv.addEvent('mouseup', this.unDrag.bind(this));
	
	headerDiv.inject(this);
};

AllColumnDivFuncs.displayAllColumnDivBody = function (){
	var allcolumns = this.options.allcolumns;
	var columns = this.options.columns;
	var body = new Element('div',{
		id: opal.generateId(),
		'class': 'bodyDiv'
	});
	
	allcolumns.each(function(column, index){
		var bodyItem = new Element('div');
		var checked = '';
		var disabled = '';
		var display = null;
		var currColumn = null;
		columns.each(function(item, index){
			var field = item.getField();
			if(field == column.getField()){
				checked = 'checked';
				display = item.getDisplay();
				currColumn = item;
				if(item.getType() != 'text'){
					disabled = 'disabled';
				}
			}
		});
		
		var checkbox = new Element('input',{
			'type': 'checkbox',
			'class': 'normalradio',
			'checked': checked,
			'disabled': disabled,
			id: column.getField() + 'bodyitemcheckbox'
		});
		
		var span = new Element('span');
		span.set('text', column.getField());
		
		var inputdisabled = '';
		
		if(checked == 'checked'){
			inputdisabled = 'disabled';
		}
		
		var input = new Element('input',{
			'type': 'text',
			'value': $pick(display, column.getField()),
			'class': 'noBoder',
			'disabled': inputdisabled,
			id: column.getField() + 'bodyiteminput'
		});
		
		if(!currColumn){
			currColumn = new opal.widget.ColumnDesc({'display': input.value, 'field': column.getField()});
		}
		
		checkbox.addEvent('click', function (){
			var checked = checkbox.checked;
			if(checked == true){
				input.disabled = 'disabled';
				if(currColumn){
					columns.include(currColumn);
				}else {
					alert('column is null');
				}
			}else {
				if(currColumn){
					columns.remove(currColumn);
				}
				input.disabled = '';
			}
		});
		
		input.addEvent('focus', function (){
			input.removeClass('noBoder');
		});
		input.addEvent('blur', function (){
			input.addClass('noBoder');
			currColumn.setDisplay(input.value);
		});
		
		checkbox.inject(bodyItem);
		span.inject(bodyItem);
		input.inject(bodyItem);
		
		bodyItem.inject(body);
	});
	
	
	
	body.inject(this);
};

AllColumnDivFuncs.displayAllColumnDivFooter = function (){
	var footer = new Element('div',{
		'class': 'footerDiv',
		id : opal.generateId()
	});
	
	var saveBt = new Element('button', {
		'value': lsave,
		'class': 'button1'
	});
	
	saveBt.addEvent('click', this.confirm.bind(this));
	
	var cancelBt = new Element('button', {
		'value': lcancel,
		'class': 'button1'
	});
	
	cancelBt.addEvent('click', this.close.bind(this));
	
	saveBt.inject(footer);
	cancelBt.inject(footer);
	
	footer.inject(this);
};

AllColumnDivFuncs.confirm = function (){
	var dbgrid = this.options.DBGrid;
	var columns = this.options.columns;
	if(dbgrid){
		if(dbgrid.options.columns != columns){
			dbgrid.options.columns = columns;
			dbgrid.reload();
		}
	}
	this.close();
};

AllColumnDivFuncs.close = function (){
	this.dispose();
};




